//
//  ListViewHeader.swift
//  MovieAppDemo
//
//  Created by yanjin on 2025/2/3.
//

import SwiftUI

struct ListViewHeader: View {
    var bannersdata: [Banner] = banners
    @Binding var type: Int
    @Binding var scrollOffset: CGFloat
    @Binding var showBarInfo: Bool
    @Binding var showSearchTags: Bool
    @Binding var tag:String
    
    var body: some View {
        Section {
            VStack(alignment: .leading, spacing: 0) {
                VStack {
                    HStack {
                        Image(bannersdata[type].image)
                            .resizable()
                            .scaledToFill()
                            .frame(width: 60, height: 60)
                            .clipShape(Circle())

                        VStack(alignment: .leading, spacing: 5) {
                            Text(bannersdata[type].title)
                                .font(.title)
                                .foregroundColor(.white)
                                .lineLimit(1)
                            Text(bannersdata[type].update)
                                .font(.subheadline)
                                .foregroundColor(.gray)
                        }
                        Spacer()
                    }

                    Text(bannersdata[type].content)
                        .font(.system(size: 18))
                        .foregroundColor(.white)
                        .opacity(0.8)
                        .lineLimit(2)
                        .multilineTextAlignment(.leading)
                        .frame(maxWidth: .infinity, alignment: .leading) // 强制文本始终左对齐，宽度最大
                        .padding(.top, 5)
                    HStack {
                        VStack(alignment: .leading) {
                            Text("\(bannersdata[type].count)")
                                .font(.title3)
                                .bold()
                                .foregroundColor(.white)
                            Text("总数")
                                .font(.caption)
                                .foregroundColor(.white)
                        }
                        .padding(5)

                        VStack(alignment: .leading) {
                            Text("\(bannersdata[type].star)")
                                .font(.title3)
                                .bold()
                                .foregroundColor(.white)
                            Text("获赞")
                                .font(.caption)
                                .foregroundColor(.white)
                        }
                        .padding(5)

                        Spacer()

                        Button {
                            showSearchTags.toggle()
                        } label: {
                            Text(tag)
                        }
                        .foregroundStyle(.black)
                        .padding(.horizontal, 10)
                        .padding(.vertical, 8)
                        .background(.white)
                        .clipShape(RoundedRectangle(cornerRadius: 5))
                    }
                }
            }
        }
        .padding()
        .background(Color("BgColor2"))
        .listRowBackground(Color.clear)
        .listRowSeparator(.hidden)
    }
}

struct ListViewHeader_Previews: PreviewProvider {
    @State static var type: Int = 3
    @State static var scrollOffset: CGFloat = 0
    @State static var showBarInfo: Bool = false
    @State static var showSearchTags: Bool = true
    @State static var tag:String = "WEBTOON"

    static var previews: some View {
        ListViewHeader(type: $type, scrollOffset: $scrollOffset, showBarInfo: $showBarInfo,showSearchTags: $showSearchTags,tag: $tag)
            .previewLayout(.sizeThatFits)
    }
}
